﻿ILParser = function (sourceCode) {
    if (!sourceCode || !sourceCode.length)
        throw "InvalidSourceException";
    
    this.sourceCode = sourceCode;
    this.index = -1;
    this.column = 0;
    this.row = 0;
};

ILParser.prototype.Parse = function ()
{
    this.parseMethod();
};

ILParser.prototype.parseMethod = function ()
{
    this.parseMethodDecl();
    this.parseMethodBodyItems();
    this.parseMethodBody();
};

ILParser.prototype.parseMethodDecl = function ()
{
    
};

ILParser.prototype.parseMethodBodyItems = function ()
{
    this.skipWhiteSpace();
    while (!this.isEOF() && this.getCurrentChar() == ".")
    {
        this.parseMethodBodyItem();
        this.skipWhiteSpace();
    }
};

ILParser.prototype.parseMethodBodyItem = function ()
{
    this.skipUntil(function(input){return input == "\n";});
    this.moveOne();
};

ILParser.prototype.parseMethodBody = function ()
{
    while (!this.isEOF() && this.getCurrentChar() != "}")
        this.parseInstructionOrLabel();
};

ILParser.prototype.parseInstructionOrLabel = function ()
{
    this.skipWhitespace();
    var firstPart;
     
    
    

};

ILParser.prototype.getCurrentChar = function ()
{
    return this.sourceCode[this.index];
};

ILParser.prototype.moveOne = function ()
{
    var currentChar = this.getCurrentChar();

    this.index++;
    
    if (this.isEOF())
        return false;
    
    if (currentChar == "\n")
    {
        this.row++;
        this.column = 0;
    }
    else
        this.column++;

    return true;
};

ILParser.prototype.isEOF = function ()
{
    return this.index >= this.sourceCode.length;
};

ILParser.prototype.skipWhiteSpace = function ()
{
    this.skipWhile(function(input){return this.isWhiteSpace(input);});
};

ILParser.prototype.skipUntil = function (evaluationFunction)
{
    this.skipWhile(function(input){return !evaluationFunction(input);});
};

ILParser.prototype.skipWhile = function (evaluationFunction)
{
    if (!evaluationFunction)
        throw "ArgumentException";
    
    if (this.isEOF())
        return;
    
    var current = this.getCurrentChar();
    while (evaluationFunction(current) && this.moveOne())
        current = this.GetCurrentChar();
};

ILParser.prototype.isWhiteSpace = function (nextChar)
{
    return nextChar == " " || nextChar == "\t";   
};

ILParser.prototype.getNextToken = function ()
{
    
};

ILParser.prototype.parseInstr = function ()
{
    
};

ILParser.prototype.parseLabel = function ()
{
    
};




ILParser.prototype.parseCustomDecl = function ()
{
    
};
ILParser.prototype.parseDataDecl = function ()
{
    
};
ILParser.prototype.parseEmitByte = function ()
{
    
};
ILParser.prototype.parseEntryPoint = function ()
{
    
};
ILParser.prototype.parseLocals = function ()
{
    
};
ILParser.prototype.parseMaxStack = function ()
{
    
};
ILParser.prototype.parseParam = function ()
{
    
};
ILParser.prototype.parseExternSourceDecl = function ()
{
    
};
ILParser.prototype.parseScopeBlock = function ()
{
    
};

ILParser.prototype.parseSecurityDecl = function ()
{
    
};

ILParser.prototype.parseSEHBlock = function ()
{
    
};





